package com.xiu.travel.handler;

import com.xiu.travel.util.IPUtils;
import com.xiu.travel.util.SecurityUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis plus 配置类
 *
 * @author yingyingying
 * @date 2023/4/3 11:55
 */
@Slf4j
@Component
public class MybatisHandler implements MetaObjectHandler
{
    @Override
    public void insertFill(MetaObject metaObject)
    {
        if (ObjectUtils.isEmpty(this.getFieldValByName("createTime",metaObject)))
        {
            this.setFieldValByName("createTime", new Date(), metaObject);
        }
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("creator", SecurityUtils.getUserId(), metaObject);
        this.setFieldValByName("ip", IPUtils.getIpAddr(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject)
    {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updater", SecurityUtils.getUserId(), metaObject);
        this.setFieldValByName("ip", IPUtils.getIpAddr(), metaObject);
    }

}
